﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CommonControl;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmWarehouse : XtraForm
    {
        public FrmWarehouse()
        {
            InitializeComponent();
            InitGrid();
        }

        private void FrmWarehouse_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            var bll = new DMZS.BLL.Warehouse();
            try
            {
                grdBody.DataSource = bll.GetListForUIExpand().Tables[0];
            }
            catch (Exception ex)
            {
                Utils.WriteTxtLog(ex);
                XtraMessageBox.Show(Utils.GetRuntimeMessage(ex), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #region 网格设置
        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvBody.IndicatorWidth = grvBody.IndicatorWidth == -1 ? 40 : grvBody.IndicatorWidth;
            grvBody.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvBody);
        }
        //双击修改
        private void grvSupplier_DoubleClick(object sender, EventArgs e)
        {
            if (grvBody.GetFocusedDataRow() == null)
            {
                return;
            }
            var frm = new FrmWarehouse_Editor();
            frm.ID = Convert.ToInt32(grvBody.GetFocusedDataRow()["ID"]);
            frm.ShowDialog();
        }
        //数字列
        private void grvSupplier_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }
        #endregion

        #region 工具栏按钮事件
        //新增
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmWarehouse_Editor();
            frm.ID = 0;
            frm.ShowDialog();
        }
        //修改
        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvBody.GetFocusedDataRow() == null)
            {
                return;
            }
            var frm = new FrmWarehouse_Editor();
            frm.ID = Convert.ToInt32(grvBody.GetFocusedDataRow()["ID"]);
            frm.ShowDialog();
        }
        //删除
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvBody.GetFocusedDataRow() == null)
            {
                return;
            }
            if (DialogResult.No == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                return;
            }
            var bll = new DMZS.BLL.Warehouse();
            bll.DeleteExpand(Convert.ToInt32(grvBody.GetFocusedDataRow()["ID"]));
            LoadData();
        }
        //刷新
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }
        #endregion

        private void grvBody_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "IsStop")
            {
                if (e.Value == DBNull.Value || Convert.ToInt32(e.Value) == 0)
                {
                    e.DisplayText = "启用";
                }
                else
                {
                    e.DisplayText = "停用";
                }
            }
        }
    }
}
